package src;

import java.io.*;
import javax.swing.*;

public class TestFileClass  {
   // execute application
   public static void main( String args[] )   {
   	
   	  String output ="";
      
      // get user input
	  String input = JOptionPane.showInputDialog("Enter Your File Name");
	  
	  // create a file object that represents the users input
	  File theFile = new File(input);
	  
	  // if name exists, output information about it
      if ( theFile.exists() ) {	
      	 
      	 output +=  theFile.getName() + " exists";
      	 output +=  "\nLast modified: " + theFile.lastModified();
      	 output +=  "\nLength: " + theFile.length();
      	 output +=  "\nPath: " + theFile.getPath();
      	 output +=  "\nParent: " + theFile.getParent();
      	 
      		// append contents of file to output string
            try {
            
              BufferedReader buffer = new BufferedReader(new FileReader( theFile ) );
              output += "\n\n";
              String text = "";
              
              //read a line at a time from the file   
              while ( ( text = buffer.readLine() ) != null ) 
                 
   					output += text + "\n";
   					
              }//end while

            // handle any errors that occur when reading from the file
            catch( IOException ioException ) {
               JOptionPane.showMessageDialog( null, "Error reading from file");
            }//end catch
            
            //output the file details and its contents
            JOptionPane.showMessageDialog(null, output);
       
       }//end if
      
      else {
      
      	 //if the file does not exist output an error message
         JOptionPane.showMessageDialog( null, input + " Does Not Exist");
         System.exit(0);
      
      }//end else   
	  
   }//end main
   
}  // end class TestFileClass